_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=6.4.0
pkgrel=1
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
url="https://www.octave.org"
license=('GPL3')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-sundials"
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         "${MINGW_PACKAGE_PREFIX}-qscintilla"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-gnuplot"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio")
optdepends=('texinfo: for help-support in Octave'
            "${MINGW_PACKAGE_PREFIX}-epstool: eps output with tight bounding box"
            "${MINGW_PACKAGE_PREFIX}-fltk: alternative plotting and dialogs"
            "${MINGW_PACKAGE_PREFIX}-gnuplot: alternative plotting"
            "${MINGW_PACKAGE_PREFIX}-portaudio: audio support")
source=(https://ftp.gnu.org/gnu/octave/octave-$pkgver.tar.gz
        "0001-binary-install-location.patch"
        "0002-mk-doc-cache-path.patch"
        "0003-pkg-octave-binary.patch"
        "0004-spawn-p_wait.patch"
        "0005-makeinfo-perl.patch")
sha512sums=('91ff031f6dfff8506fa738fd4f1f07276501fdfe003f8ed992cccf91da14d9d33da6d08923322b0dae5f5b696b49856b0f5c5065e26b0701b0d1630173807435'
            'dc714dbf04dc551cd996b4b1b9bf7816100103fa8c3d7d3633171d36f5d8eb20464fa0cfeee387c882a1fcb81e824d11e68b7f99f2f6b56d1f2ef6b220c8acab'
            '5742cde34ae9184276c830dab41dccb289991578656353658bf07315d89b1c3870c6c51520d5aae0cec8c7fb12c707a1970aa9dde353b4ab54e515d1d3cf66e0'
            '96c596fb105589bbfc662211cd62c2ecfea903656b15b54045a6ee852d8c3b3bdc6894bb2f93693f88bb5eab6ad7ed54056b42a0f3cc3832e2be961887e10acf'
            '1dd71764e3ad88c3c7e3bcfd24a0d4fd1873496a4564f7e0817230f73957345776c43f41bb1c394613cacf48b0d741555f99240047ae285cb8461031e8ac7bb8'
            'dc99ad8eb214746633d14ce09edddc20f6dd73929f1c556538157e5e4d7210b4045b35a42bd70ebb31c0652aca6c53c720178826b125b19c5cfde9fe8e6c36f8')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-binary-install-location.patch"
  patch -p1 -i "${srcdir}/0002-mk-doc-cache-path.patch"
  patch -p1 -i "${srcdir}/0003-pkg-octave-binary.patch"
  patch -p1 -i "${srcdir}/0004-spawn-p_wait.patch"
  patch -p1 -i "${srcdir}/0005-makeinfo-perl.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p build-${MSYSTEM}
  cd build-${MSYSTEM}

  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --enable-shared --disable-static \
    --disable-docs \
    --enable-relocate-all \
    --enable-link-all-dependencies \
    --with-blas="-lblas" \
    ac_cv_search_tputs=-ltermcap \
    JAVA_HOME=""

  PERL5SHELL="bash -l -c" make all
}

package() {
  make -C "build-${MSYSTEM}" DESTDIR="${pkgdir}" install

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
